package cn.cmcc.online.smsapi.sdk.queue;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import org.json.JSONObject;

import cn.cmcc.online.smsapi.sdk.constant.Constant;
import cn.cmcc.online.smsapi.sdk.db.entity.MatchCacheManager;
import cn.cmcc.online.smsapi.sdk.log.LogManager;
import cn.cmcc.online.smsapi.sdk.util.JsonUtil;
import cn.cmcc.online.smsapi.sdk.util.SdkCache;
import cn.cmcc.online.smsapi.sdk.util.StringUtils;
import cn.cmcc.online.smsapi.util.ParseBubbleManager;
import cn.cmcc.online.smsapi.util.ParseManager;
import cn.cmcc.online.smsapi.util.ParseRichBubbleManager;


public class RichBubbleTaskQueue {
    public static BlockingQueue<JSONObject> queue = new LinkedBlockingQueue<JSONObject>();// 阻塞队列

    /**
     * 
     * @param dataStatu
     *            default 0
     * @param msgId
     * @param phoneNum
     * @param smsContent
     * @param centerNum
     * @param dataType
     *            1:simple bubble 2:rich bubble 3:all
     * @param res
     */
    public static void addDataToQueue(int dataStatu, String msgId,
            String phoneNum, String smsContent, String centerNum, int dataType,
            long smsReceiveTime, JSONObject res) {
        if (res == null)
            res = new JSONObject();
        try {
            res.put("dataStatu", dataStatu);
            res.put("msg_id", msgId);
            res.put("phoneNum", phoneNum);
            res.put("smsContent", smsContent);
            res.put("smsReceiveTime", smsReceiveTime);
            if (centerNum != null)
                res.put("centerNum", centerNum);
            res.put("dataType", dataType);
            queue.put(res);
            startTaskQueue();
        } catch (Throwable e) {
//            e.printStackTrace();
            LogManager.e(Constant.TAG, "addDataToQueue: ", e);

        }

    }

    static Thread queueThread = null;

    public synchronized static void startTaskQueue() {
        if (queueThread == null) {
            queueThread = new Thread() {
                public void run() {
                	try {
						this.setName("xiaoyuan_bubbletaskqueue");
                	android.os.Process.setThreadPriority(SdkTaskQueue.XIAOYUAN_THREA_PRIORITY);
                    JSONObject task = null;
                    while (true) {
                        try {
                            task = queue.take();
                            if (task != null) {

                                Integer dataStatu = (Integer) JsonUtil
                                        .getValueFromJsonObject(task,
                                                "dataStatu");
                                if (dataStatu != null) {
                                    Integer dataType = (Integer) JsonUtil
                                            .getValueFromJsonObject(task,
                                                    "dataType");
                                    if (dataType != null) {
                                        if (dataType == 2) {

                                            boolean needParseMsg = false;
                                            if (dataStatu == 2) {
                                                String saveTime = (String) JsonUtil
                                                        .getValueFromJsonObject(
                                                                task,
                                                                "save_time");
                                                if (!StringUtils
                                                        .isNull(saveTime)) {
                                                    long time = Long
                                                            .valueOf(saveTime);
                                                    long time1 = System
                                                            .currentTimeMillis();
                                                    if ((time1 - time) > 2592000L) {
                                                        needParseMsg = true;
                                                    }
                                                }
                                            } else {
                                                needParseMsg = true;
                                            }
                                            if (needParseMsg) {
                                                Map<String, Object> data = ParseManager
                                                        .parseMsgToBubbleCardResult(
                                                                Constant.getContext(),
                                                                (String) JsonUtil
                                                                        .getValueFromJsonObject(
                                                                                task,
                                                                                "msg_id"),
                                                                (String) JsonUtil
                                                                        .getValueFromJsonObject(
                                                                                task,
                                                                                "phoneNum"),
                                                                (String) JsonUtil
                                                                        .getValueFromJsonObject(
                                                                                task,
                                                                                "centerNum"),
                                                                (String) JsonUtil
                                                                        .getValueFromJsonObject(
                                                                                task,
                                                                                "smsContent"),
                                                                (Long) JsonUtil
                                                                        .getValueFromJsonObject(
                                                                                task,
                                                                                "smsReceiveTime"),
                                                                (byte) 1, null);
                                                if (data != null) {
                                                    Long id = (Long) data
                                                            .get("CACHE_SDK_MSG_ID");
                                                    if (id != null) {
                                                        Map<String, JSONObject> res = MatchCacheManager
                                                                .loadDataByParam(
                                                                        "id=?",
                                                                        new String[] { id
                                                                                + "" });
                                                        SdkCache.putCacheRichBubbleData( (String) JsonUtil
                                                                .getValueFromJsonObject(
                                                                        task,
                                                                        "phoneNum"), res);
//                                                        if (res != null) {
//                                                            if (ParseRichBubbleManager.cacheRichBubbleData != null) {
//                                                                ParseRichBubbleManager.cacheRichBubbleData
//                                                                        .putAll(res);
//                                                            } else {
//                                                                ParseRichBubbleManager.cacheRichBubbleData = res;
//                                                            }
//                                                        }
                                                    }
                                                }
                                            }
                                        } else if (dataType == 1) {
                                            // simple bubble data
                                            boolean needParseMsg = false;
                                            if (dataStatu == 2) {
                                                String saveTime = (String) JsonUtil
                                                        .getValueFromJsonObject(
                                                                task,
                                                                "session_lasttime");
                                                if (!StringUtils
                                                        .isNull(saveTime)) {
                                                    long time = Long
                                                            .valueOf(saveTime);
                                                    long time1 = System
                                                            .currentTimeMillis();
                                                    if ((time1 - time) > 2592000L) {
                                                        needParseMsg = true;
                                                    }
                                                }
                                            } else {
                                                needParseMsg = true;
                                            }
                                            if (needParseMsg) {
                                                /*SDK-719 zhaojiangwei 20170117 start */
                                                Map<String, String> extend = new HashMap<String, String>();
                                                extend.put("msgTime", (String) JsonUtil.getValueFromJsonObject(task,"smsReceiveTime"));
                                                Map<String, Object> data = ParseRichBubbleManager
                                                        .parseMsgToSimpleBubbleResult(Constant.getContext(),
                                                                (String) JsonUtil.getValueFromJsonObject(task,
                                                                        "msg_id"),
                                                                (String) JsonUtil.getValueFromJsonObject(task,
                                                                        "phoneNum"),
                                                                (String) JsonUtil.getValueFromJsonObject(task,
                                                                        "centerNum"),
                                                                (String) JsonUtil.getValueFromJsonObject(task,
                                                                        "smsContent"),
                                                                (byte) 1, extend);
                                                /*SDK-719 zhaojiangwei 20170117 end */
                                                if (data != null) {
                                                    Long id = (Long) data
                                                            .get("CACHE_SDK_MSG_ID");
                                                    if (id != null && id > -1) {
                                                        Map<String, JSONObject> res = MatchCacheManager
                                                                .loadDataByParam(
                                                                        "id=?",
                                                                        new String[] { id
                                                                                + "" });
                                                        SdkCache.putCacheBubbleData(
                                                        		(String) JsonUtil.getValueFromJsonObject(task,"phoneNum"), res);
//                                                        if (res != null) {
//                                                            if (ParseBubbleManager.cacheBubbleData != null) {
//                                                                ParseBubbleManager.cacheBubbleData
//                                                                        .putAll(res);
//                                                            } else {
//                                                                ParseBubbleManager.cacheBubbleData = res;
//                                                            }
//                                                        }
                                                    }
                                                }
                                            }
                                        } else if (dataType == 3) {
                                            boolean needParseMsg = false;
                                            if (dataStatu == 2) {
                                                String saveTime = (String) JsonUtil
                                                        .getValueFromJsonObject(
                                                                task,
                                                                "session_lasttime");
                                                if (!StringUtils
                                                        .isNull(saveTime)) {
                                                    long time = Long
                                                            .valueOf(saveTime);
                                                    long time1 = System
                                                            .currentTimeMillis();
                                                    if ((time1 - time) > 2592000L) {
                                                        needParseMsg = true;
                                                    }
                                                }
                                            } else {
                                                needParseMsg = true;
                                            }
                                            if (needParseMsg) {
                                                Map<String, Object> data = ParseRichBubbleManager
                                                        .parseMsgToSimpleBubbleResult(
                                                                Constant.getContext(),
                                                                (String) JsonUtil
                                                                        .getValueFromJsonObject(
                                                                                task,
                                                                                "msg_id"),
                                                                (String) JsonUtil
                                                                        .getValueFromJsonObject(
                                                                                task,
                                                                                "phoneNum"),
                                                                (String) JsonUtil
                                                                        .getValueFromJsonObject(
                                                                                task,
                                                                                "centerNum"),
                                                                (String) JsonUtil
                                                                        .getValueFromJsonObject(
                                                                                task,
                                                                                "smsContent"),
                                                                (byte) 1, null);
                                                if (data != null) {
                                                    Long id = (Long) data
                                                            .get("CACHE_SDK_MSG_ID");
                                                    if (id != null && id > -1) {
                                                        Map<String, JSONObject> res = MatchCacheManager
                                                                .loadDataByParam(
                                                                        "id=?",
                                                                        new String[] { id
                                                                                + "" });
                                                        SdkCache.putCacheBubbleData(
                                                        		(String) JsonUtil.getValueFromJsonObject(task,"phoneNum"), res);
//                                                        if (res != null) {
//                                                            if (ParseBubbleManager.cacheBubbleData != null) {
//                                                                ParseBubbleManager.cacheBubbleData
//                                                                        .putAll(res);
//                                                            } else {
//                                                                ParseBubbleManager.cacheBubbleData = res;
//                                                            }
//                                                        }
                                                    }
                                                }
                                            }

                                            if (dataStatu == 2) {
                                                String saveTime = (String) JsonUtil
                                                        .getValueFromJsonObject(
                                                                task,
                                                                "save_time");
                                                if (!StringUtils
                                                        .isNull(saveTime)) {
                                                    long time = Long
                                                            .valueOf(saveTime);
                                                    long time1 = System
                                                            .currentTimeMillis();
                                                    if ((time1 - time) > 2592000L) {
                                                        needParseMsg = true;
                                                    }
                                                }
                                            } else {
                                                needParseMsg = true;
                                            }
                                            if (needParseMsg) {
                                                Map<String, Object> data = ParseManager
                                                        .parseMsgToBubbleCardResult(
                                                                Constant.getContext(),
                                                                (String) JsonUtil
                                                                        .getValueFromJsonObject(
                                                                                task,
                                                                                "msg_id"),
                                                                (String) JsonUtil
                                                                        .getValueFromJsonObject(
                                                                                task,
                                                                                "phoneNum"),
                                                                (String) JsonUtil
                                                                        .getValueFromJsonObject(
                                                                                task,
                                                                                "centerNum"),
                                                                (String) JsonUtil
                                                                        .getValueFromJsonObject(
                                                                                task,
                                                                                "smsContent"),
                                                                (Long) JsonUtil
                                                                        .getValueFromJsonObject(
                                                                                task,
                                                                                "smsReceiveTime"),
                                                                (byte) 1, null);
                                                if (data != null) {
                                                    Long id = (Long) data
                                                            .get("CACHE_SDK_MSG_ID");
                                                    if (id != null) {
                                                        Map<String, JSONObject> res = MatchCacheManager
                                                                .loadDataByParam(
                                                                        "id=?",
                                                                        new String[] { id
                                                                                + "" });
                                                        SdkCache.putCacheRichBubbleData( (String) JsonUtil
                                                                .getValueFromJsonObject(
                                                                        task,
                                                                        "phoneNum"), res);
//                                                        if (res != null) {
//                                                            if (ParseRichBubbleManager.cacheRichBubbleData != null) {
//                                                                ParseRichBubbleManager.cacheRichBubbleData
//                                                                        .putAll(res);
//                                                            } else {
//                                                                ParseRichBubbleManager.cacheRichBubbleData = res;
//                                                            }
//                                                        }
                                                    }
                                                }
                                            }

                                        }

                                    }
                                }

                            }
                        } catch (Throwable e) {
                            LogManager.e("RichBubbleTaskQueue",
                                    e.getLocalizedMessage(),e);
                        }
                    }
                
					} catch (Throwable e) {
						LogManager.e(Constant.TAG, e.getMessage(), e);
					}
                	};
            };
            queueThread.start();
        }

    }
}
